<template>
    <div>
      <!-- 面包屑组件-->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
      <!--卡片视图区域 -->
      <el-card>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="goAddpage">添加商品</el-button>
          </el-col>
        </el-row>
        <!--table表格区域 -->
        <el-table :data="goodsList" stripe border>
          <el-table-column type="index"></el-table-column>
          <el-table-column label="商品名称" prop="goods_name"></el-table-column>
          <el-table-column label="商品价格(元)" prop="goods_price" width="95px"></el-table-column>
          <el-table-column label="商品数量" prop="goods_number" width="70px"></el-table-column>
          <el-table-column label="创建时间" prop="add_time" width="140px">
            <template slot-scope="scope">
              {{scope.row.add_time*1000 | dataFormat}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="130px">
            <template  slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini"
              @click="showEditDialog(scope.row.goods_id)"></el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="removeById(scope.row.goods_id)"></el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10, 15, 20, 25]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-card>

      <!--编辑商品区域 -->
      <el-dialog
        title="修改商品信息"
        :visible.sync="editDialogVisible"
        width="50%"
        >
        <el-form :model="editCateForm" :rules="editCateFormRules" ref="editCateFormRef" label-width="70px">
          <el-form-item label="商品名称">
            <el-input v-model="editCateForm.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="editCateForm.goods_price" type="number"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="goods_number">
            <el-input v-model="editCateForm.goods_number" type="number"></el-input>
          </el-form-item>
          <el-form-item label="创建时间" prop="add_time">
              <el-input v-model="editCateForm.add_time"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
		    <el-button @click="editDialogVisible = false">取 消</el-button>
		    <el-button type="primary" @click="editCateInfo">确 定</el-button>
		  </span>
      </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "List",
       data() {
          return {
            queryInfo: {
              query: '',
              pagenum: 1,
              pagesize: 10
            },
            //商品列表
            goodsList: [],
            total: 0,

            editCateForm:{},
            editDialogVisible: false,
            editCateFormRules: {
              goods_name: [{required: true, message: '请输入商品名字', trigger: 'blur'}],
              goods_price: [{required: true, message: '请输入商品价格', trigger: 'blur'}],
              goods_number: [{required: true, message: '请输入商品数量', trigger: 'blur'}],
              add_time: [{required: true, message: '请输入创建时间', trigger: 'blur'}],
            }

          }
       },
      created() {
          this.getGoodsList()
      },
      methods: {
        async getGoodsList() {
         const {data:res} = await this.$http.get('goods',{params: this.queryInfo})
          if(res.meta.status !== 200){
            return this.$message.error('获取商品列表失败！')
          }
          //console.log(res.data)
          this.$message.success('获取商品列表成功！')
          this.goodsList = res.data.goods
          this.total = res.data.total
        },
        //获取分页的新页大小
        handleSizeChange(newSize) {
          this.queryInfo.pagesize = newSize
          this.getGoodsList()
        },
        //获取当前页数
        handleCurrentChange(newPageNum){
          this.queryInfo.pagenum = newPageNum
          this.getGoodsList()
        },
        //根据id查询商品
        async showEditDialog(goods_id) {
          this.editDialogVisible = true
          const {data: res} = await this.$http.get('goods/'+goods_id)
          if(res.meta.status !== 201){
            this.$message.error('查询商品信息失败！')
          }
          //console.log(res.data)
          this.editCateForm = res.data
        },
        //编辑商品
        async editCateInfo() {
          this.$refs.editCateFormRef.validate(async valid =>{
            //console.log(valid)
            if(!valid) return
            //发起网络请求，修改数据
            const {data: res} = await this.$http.put('goods/'+this.editCateForm.goods_id,
              {
                goods_name: this.editCateForm.goods_name,
                goods_price: this.editCateForm.goods_price,
                goods_number: this.editCateForm.goods_number,
                goods_weight: this.editCateForm.goods_weight,
                goods_introduce: this.editCateForm.goods_introduce,
                pics: this.editCateForm.pics,
                attrs: this.editCateForm.attrs
              })
            if(res.meta.status !== 201){
              //console.log(res.meta.status)
              return this.$message.error("修改商品信息失败！")
            }
            //console.log(res.data)
            this.editDialogVisible = false
            this.getGoodsList()
            this.$message.success("修改商品信息成功！")

          })
        },
        //删除商品
         async removeById(id) {
          //弹框提示是否删除
          const confirmGoodsResult = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(err => err)
          //如果用户确认删除 返回confirm
          //如果用户取消删除 返回cancle
          //console.log(confirmGoodsResult)
          if(confirmGoodsResult !== 'confirm'){
            return this.$message.info("已取消删除！")
          }
          const {data: res} = await this.$http.delete(`goods/${id}`)
          if(res.meta.status !== 200){
            return this.$message.error("删除商品失败！")
          }
          this.getGoodsList()
          //this.$message.success("删除商品成功！")

        },
        //添加商品跳转
        goAddpage() {
          this.$router.push('goods/add')
        }

      }

    }
</script>

<style lang="less" scoped>

</style>
